package com.kmproject.haifu.mvp.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.blankj.utilcode.util.ScreenUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DataHelper;
import com.jess.arms.widget.alertview.AlertView;
import com.jess.arms.widget.alertview.AlertViewOnItemClickListener;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.AppConstant;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.di.component.DaggerMyRentObtainedComponent;
import com.kmproject.haifu.mvp.contract.MyRentObtainedContract;
import com.kmproject.haifu.mvp.model.entity.ResultBean;
import com.kmproject.haifu.mvp.model.entity.beans.HousePayWayBean;
import com.kmproject.haifu.mvp.model.entity.beans.HouseRentListShowInfoBean;
import com.kmproject.haifu.mvp.model.entity.beans.MyStoreMainBean;
import com.kmproject.haifu.mvp.model.entity.beans.ObtainedHouseBean;
import com.kmproject.haifu.mvp.model.entity.beans.UserBean;
import com.kmproject.haifu.mvp.model.entity.resbean.HouseParmarBean;
import com.kmproject.haifu.mvp.presenter.MyRentObtainedPresenter;
import com.kmproject.haifu.mvp.ui.activity.MyStoreLookObtainedActivity;
import com.kmproject.haifu.mvp.ui.activity.SecondhandObtainedActivity;
import com.kmproject.haifu.mvp.ui.adapter.ObtainedHouseAdapter;
import com.kmproject.haifu.mvp.ui.widget.ScaleTransitionPagerTitleView;

import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 09/06/2019 01:14
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class MyRentObtainedFragment extends BaseFragment<MyRentObtainedPresenter> implements MyRentObtainedContract.View {

    @BindView(R.id.mystore_head_typeselect_RadioButton1)
    RadioButton mystoreHeadTypeselectRadioButton1;
    @BindView(R.id.mystore_head_typeselect_RadioButton2)
    RadioButton mystoreHeadTypeselectRadioButton2;
    @BindView(R.id.mystore_head_typeselect_RadioGroup)
    RadioGroup mystoreHeadTypeselectRadioGroup;
    @BindView(R.id.ll_head_type_select_layout)
    LinearLayout llHeadTypeSelectLayout;
    @BindView(R.id.mystore_story_middle_lay)
    LinearLayout mystoreStoryMiddleLay;
    @BindView(R.id.mystore_story_middle_RecyclerView)
    RecyclerView mystoreStoryMiddleRecyclerView;
    Unbinder unbinder;

    private List<HousePayWayBean> mhousePayWayBeans;//房屋类型，居民住宅、商品房屋
    private HouseParmarBean houseParmarBean=new HouseParmarBean();
    private ObtainedHouseAdapter obtainedHouseAdapter;
    private int selectType=0;
    UserBean userBean;
    public static MyRentObtainedFragment newInstance() {
        MyRentObtainedFragment fragment = new MyRentObtainedFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerMyRentObtainedComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_my_rent_obtained, container, false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        if (getActivity().getIntent().hasExtra("HousePayWayBean"))
        {
            mhousePayWayBeans=(List<HousePayWayBean>)getActivity().getIntent().getSerializableExtra("HousePayWayBean");
            houseParmarBean.setHouseCategory(mhousePayWayBeans.get(0).getCategory());
        }

        if (mhousePayWayBeans!=null&&!mhousePayWayBeans.isEmpty())
        {
            houseParmarBean.setHouseCategory(mhousePayWayBeans.get(selectType).getCategory());
        }
        mystoreStoryMiddleRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        obtainedHouseAdapter=new ObtainedHouseAdapter(new ArrayList<>(), new ObtainedHouseAdapter.ItemClickCallBack() {
            @Override
            public void Callback(HouseRentListShowInfoBean houseRentListShowInfoBean) {
                new AlertView("提示", "确定删除此内容？", "取消", new String[]{"删除"}, null, getActivity(), AlertView.Style.Alert, new AlertViewOnItemClickListener() {

                    @Override
                    public void onItemClick(Object o, int position) {
                        if (position == AlertView.CANCELPOSITION) {
                            //点击取消按钮

                        }else {
                         mPresenter.removeStore(houseRentListShowInfoBean.getRentId());
                        }
                    }
                }).show();
            }
        });
        mystoreStoryMiddleRecyclerView.setAdapter(obtainedHouseAdapter);
        obtainedHouseAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);
        houseParmarBean.setLimit(AppConstant.pageSize_big);
            userBean = DataHelper.getDeviceData(getActivity(), AppConstant.USER_INFO);//获取用户信息
            mPresenter.listHouseRentShelvesForUser(userBean.getUserId(),houseParmarBean);

        mystoreHeadTypeselectRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId)
                {
                    case R.id.mystore_head_typeselect_RadioButton1:
                        selectType=0;
                        if (mhousePayWayBeans!=null&&!mhousePayWayBeans.isEmpty())
                        {
                            houseParmarBean.setHouseCategory(mhousePayWayBeans.get(selectType).getCategory());
                        }
                        obtainedHouseAdapter.setNewData(null);
                        houseParmarBean.setPage(1);


                            houseParmarBean.setHouseCategory(mhousePayWayBeans.get(0).getCategory());
                            mPresenter.listHouseRentShelvesForUser(userBean.getUserId(),houseParmarBean);


                        break;
                    case R.id.mystore_head_typeselect_RadioButton2:
                        selectType=1;
                        if (mhousePayWayBeans!=null&&!mhousePayWayBeans.isEmpty())
                        {
                            houseParmarBean.setHouseCategory(mhousePayWayBeans.get(selectType).getCategory());
                        }
                        obtainedHouseAdapter.setNewData(null);
                        houseParmarBean.setPage(1);

                            houseParmarBean.setHouseCategory(mhousePayWayBeans.get(1).getCategory());
                            mPresenter.listHouseRentShelvesForUser(userBean.getUserId(),houseParmarBean);


                        break;
                }

            }
        });
    }

    /**
     * 通过此方法可以使 Fragment 能够与外界做一些交互和通信, 比如说外部的 Activity 想让自己持有的某个 Fragment 对象执行一些方法,
     * 建议在有多个需要与外界交互的方法时, 统一传 {@link Message}, 通过 what 字段来区分不同的方法, 在 {@link #setData(Object)}
     * 方法中就可以 {@code switch} 做不同的操作, 这样就可以用统一的入口方法做多个不同的操作, 可以起到分发的作用
     * <p>
     * 调用此方法时请注意调用时 Fragment 的生命周期, 如果调用 {@link #setData(Object)} 方法时 {@link Fragment#onCreate(Bundle)} 还没执行
     * 但在 {@link #setData(Object)} 里却调用了 Presenter 的方法, 是会报空的, 因为 Dagger 注入是在 {@link Fragment#onCreate(Bundle)} 方法中执行的
     * 然后才创建的 Presenter, 如果要做一些初始化操作,可以不必让外部调用 {@link #setData(Object)}, 在 {@link #initData(Bundle)} 中初始化就可以了
     * <p>
     * Example usage:
     * <pre>
     * public void setData(@Nullable Object data) {
     *     if (data != null && data instanceof Message) {
     *         switch (((Message) data).what) {
     *             case 0:
     *                 loadData(((Message) data).arg1);
     *                 break;
     *             case 1:
     *                 refreshUI();
     *                 break;
     *             default:
     *                 //do something
     *                 break;
     *         }
     *     }
     * }
     *
     * // call setData(Object):
     * Message data = new Message();
     * data.what = 0;
     * data.arg1 = 1;
     * fragment.setData(data);
     * </pre>
     *
     * @param data 当不需要参数时 {@code data} 可以为 {@code null}
     */
    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void showLoading() {
        loadingDialog.show();
    }

    @Override
    public void hideLoading() {
        loadingDialog.dismiss();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }


    @Override
    public void listHouseRentShelvesSuccess(ResultBean<List<ObtainedHouseBean>> obtainedHouseBeans) {
        if ((obtainedHouseBeans==null||obtainedHouseBeans.getData()==null||obtainedHouseBeans.getData().isEmpty())&&obtainedHouseAdapter.getData().isEmpty())
        {
            obtainedHouseAdapter.setEmptyView(AppUtil.setEmptyView(getActivity(),"未查询到下架信息","",null,true));
            return;
        }

        obtainedHouseAdapter.setNewData(obtainedHouseBeans.getData());
    }

    @Override
    public void removeStoreSuccess() {
        //删除房源
        mystoreStoryMiddleRecyclerView.post(new Runnable() {
            @Override
            public void run() {
                showMessage("删除成功");
            }
        });

        houseParmarBean=new HouseParmarBean();
        if (mhousePayWayBeans!=null&&!mhousePayWayBeans.isEmpty())
        {
            houseParmarBean.setHouseCategory(mhousePayWayBeans.get(selectType).getCategory());
        }
        obtainedHouseAdapter.setNewData(new ArrayList<>());
            UserBean userBean = DataHelper.getDeviceData(getActivity(), AppConstant.USER_INFO);//获取用户信息
            mPresenter.listHouseRentShelvesForUser(userBean.getUserId(),houseParmarBean);



    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
